<?php

namespace app\common\model;

use think\Model;

/**
 * 会员余额日志模型
 */
class MoneyLog Extends Model
{

    // 表名
    protected $name = 'user_money_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
    ];

    /**
     * 分页列表
     */
    public static function getList($postData)
    {
        $where = '1=1';
        $order = 'a.id desc';
        $page = isset($postData['page']) && !empty($postData['page']) ? intval($postData['page']) : 1;
        $limit = isset($postData['limit']) && !empty($postData['limit']) ? intval($postData['limit']) : 10;
        $start = ($page - 1) * $limit;
        $field = "a.*, b.mobile, b.username";

        if(isset($postData['user_id']) && !empty($postData['user_id']))
        {
            $where .= " and a.user_id={$postData['user_id']}";
        }
        if(isset($postData['username']) && !empty($postData['username']))
        {
            $where .= " and b.username like '%{$postData['username']}%'";
        }
        if(isset($postData['mobile']) && !empty($postData['mobile']))
        {
            $where .= " and b.mobile like '%{$postData['mobile']}%'";
        }

        $list = db('user_money_log')
            ->alias('a')
            ->join('user b', 'a.user_id = b.id')
            ->where($where)
            ->order($order)
            ->limit($start, $limit)
            ->field($field)
            ->select();
        if (!empty($list)) {
            foreach ($list as &$v) {
                $v['createtimeText'] = isset($v['createtime']) && !empty($v['createtime']) ? date('Y-m-d H:i:s', $v['createtime']) : '';
            }
            unset($v);
        }

        $total = db('user_money_log')
            ->alias('a')
            ->join('user b', 'a.user_id = b.id')
            ->where($where)
            ->count();

        return [
            'list' => $list,
            'total' => $total,
        ];

    }

}
